/*
 *   Copyright (C) 2009,2010,2014 by Jonathan Naylor G4KLX
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "DVToolWriterPreferences.h"
#include "DVToolWriterFrame.h"
#include "DVTOOLFileWriter.h"
#include "DVToolWriterApp.h"
#include "AMBEFileWriter.h"
#include "DStarDefines.h"
#include "MessageEvent.h"
#include "HeaderEvent.h"
#include "ErrorEvent.h"
#include "Version.h"

#include <wx/gbsizer.h>
#include <wx/aboutdlg.h>
#include <wx/filename.h>

unsigned char startImage[] = {
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
};

unsigned char startAlpha[] = {
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0x00U, 0x00U,
	0x00U, 0x00U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0x00U, 0x00U,
	0x00U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0x00U,
	0x00U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0x00U,
	0x00U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0x00U,
	0x00U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0x00U,
	0x00U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0x00U,
	0x00U, 0x00U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0x00U, 0x00U,
	0x00U, 0x00U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
};

unsigned char stopImage[] = {
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
	0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U
};

enum {
	Menu_File_File = 6000,

	Menu_Edit_Preferences,

	Button_Start,
	Button_Stop
};

DEFINE_EVENT_TYPE(ERROR_EVENT)
DEFINE_EVENT_TYPE(FINISHED_EVENT)

BEGIN_EVENT_TABLE(CDVToolWriterFrame, wxFrame)
	EVT_MENU(Menu_File_File, CDVToolWriterFrame::onOpen)
	EVT_MENU(wxID_EXIT, CDVToolWriterFrame::onQuit)

	EVT_MENU(Menu_Edit_Preferences, CDVToolWriterFrame::onPreferences)

	EVT_MENU(wxID_ABOUT, CDVToolWriterFrame::onAbout)

	EVT_CLOSE(CDVToolWriterFrame::onClose)

	EVT_BUTTON(Button_Start, CDVToolWriterFrame::onStart)
	EVT_BUTTON(Button_Stop,  CDVToolWriterFrame::onStop)

	EVT_CUSTOM(ERROR_EVENT,    wxID_ANY, CDVToolWriterFrame::onError)
	EVT_CUSTOM(FINISHED_EVENT, wxID_ANY, CDVToolWriterFrame::onFinished)
END_EVENT_TABLE()

CDVToolWriterFrame::CDVToolWriterFrame(const wxString& title) :
wxFrame(NULL, -1, title),
m_format(NULL),
m_recording(NULL),
m_your(NULL),
m_rpt1(NULL),
m_rpt2(NULL),
m_message(NULL)
{
	SetMenuBar(createMenuBar());

	wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);

	wxPanel* panel = new wxPanel(this);

	wxGridBagSizer* panelSizer = new wxGridBagSizer(BORDER_SIZE, BORDER_SIZE);

	wxStaticText* formatLabel = new wxStaticText(panel, -1, _("Format:"), wxDefaultPosition, wxSize(LABEL_WIDTH, -1), wxALIGN_RIGHT);
	panelSizer->Add(formatLabel, wxGBPosition(0, 0), wxDefaultSpan, wxALL, BORDER_SIZE);

	m_format = new wxChoice(panel, -1, wxDefaultPosition, wxSize(CONTROL_WIDTH, -1));
	m_format->Append(wxT(".dvtool"));
	m_format->Append(wxT(".ambe"));
	panelSizer->Add(m_format, wxGBPosition(0, 1), wxDefaultSpan, wxALL, BORDER_SIZE);
	m_format->SetSelection(0);

	wxImage* start = new wxImage(15, 15, startImage, startAlpha, true);
	wxImage* stop  = new wxImage(15, 15, stopImage, true);

	wxStaticText* micLabel = new wxStaticText(panel, -1, _("Microphone:"), wxDefaultPosition, wxSize(LABEL_WIDTH, -1), wxALIGN_RIGHT);
	panelSizer->Add(micLabel, wxGBPosition(0, 2), wxDefaultSpan, wxALL, BORDER_SIZE);

	wxBoxSizer* micSizer = new wxBoxSizer(wxHORIZONTAL);

	wxButton* startButton = new wxBitmapButton(panel, Button_Start, wxBitmap(*start), wxDefaultPosition, wxSize(MIC_WIDTH, -1), wxALIGN_RIGHT);
	micSizer->Add(startButton, wxALL, BORDER_SIZE);

	wxButton* stopButton = new wxBitmapButton(panel, Button_Stop, wxBitmap(*stop), wxDefaultPosition, wxSize(MIC_WIDTH, -1), wxALIGN_RIGHT);
	micSizer->Add(stopButton, wxALL, BORDER_SIZE);

	panelSizer->Add(micSizer, wxGBPosition(0, 3), wxDefaultSpan, wxALL, BORDER_SIZE);

	m_recording = new wxStaticText(panel, -1, wxEmptyString, wxDefaultPosition, wxSize(LABEL_WIDTH, -1), wxALIGN_RIGHT);
	panelSizer->Add(m_recording, wxGBPosition(0, 5), wxDefaultSpan, wxALL, BORDER_SIZE);

	wxStaticText* yourLabel = new wxStaticText(panel, -1, _("Your:"), wxDefaultPosition, wxSize(LABEL_WIDTH, -1), wxALIGN_RIGHT);
	panelSizer->Add(yourLabel, wxGBPosition(1, 0), wxDefaultSpan, wxALL, BORDER_SIZE);

	m_your = new CCallsignTextCtrl(panel, -1, wxEmptyString, wxDefaultPosition, wxSize(CONTROL_WIDTH, -1));
	m_your->SetMaxLength(LONG_CALLSIGN_LENGTH);
	panelSizer->Add(m_your, wxGBPosition(1, 1), wxDefaultSpan, wxALL, BORDER_SIZE);

	wxStaticText* rpt1Label = new wxStaticText(panel, -1, _("RPT1:"), wxDefaultPosition, wxSize(LABEL_WIDTH, -1), wxALIGN_RIGHT);
	panelSizer->Add(rpt1Label, wxGBPosition(1, 2), wxDefaultSpan, wxALL, BORDER_SIZE);

	m_rpt1 = new CCallsignTextCtrl(panel, -1, wxEmptyString, wxDefaultPosition, wxSize(CONTROL_WIDTH, -1));
	m_rpt1->SetMaxLength(LONG_CALLSIGN_LENGTH);
	panelSizer->Add(m_rpt1, wxGBPosition(1, 3), wxDefaultSpan, wxALL, BORDER_SIZE);

	wxStaticText* rpt2Label = new wxStaticText(panel, -1, _("RPT2:"), wxDefaultPosition, wxSize(LABEL_WIDTH, -1), wxALIGN_RIGHT);
	panelSizer->Add(rpt2Label, wxGBPosition(1, 4), wxDefaultSpan, wxALL, BORDER_SIZE);

	m_rpt2 = new CCallsignTextCtrl(panel, -1, wxEmptyString, wxDefaultPosition, wxSize(CONTROL_WIDTH, -1));
	m_rpt2->SetMaxLength(LONG_CALLSIGN_LENGTH);
	panelSizer->Add(m_rpt2, wxGBPosition(1, 5), wxDefaultSpan, wxALL, BORDER_SIZE);

	wxStaticText* messageLabel = new wxStaticText(panel, -1, _("Message:"), wxDefaultPosition, wxSize(LABEL_WIDTH, -1), wxALIGN_RIGHT);
	panelSizer->Add(messageLabel, wxGBPosition(2, 0), wxDefaultSpan, wxALL, BORDER_SIZE);

	m_message = new wxTextCtrl(panel, -1, wxEmptyString, wxDefaultPosition, wxSize(CONTROL_WIDTH * 2U + LABEL_WIDTH + BORDER_SIZE * 6U, -1), wxALIGN_LEFT);
	m_message->SetMaxLength(MESSAGE_LENGTH);
	panelSizer->Add(m_message, wxGBPosition(2, 1), wxGBSpan(1, 3), wxALL, BORDER_SIZE);

	panel->SetSizer(panelSizer);
	panelSizer->SetSizeHints(panel);

	mainSizer->Add(panel);

	SetSizer(mainSizer);
	mainSizer->SetSizeHints(this);
}

CDVToolWriterFrame::~CDVToolWriterFrame()
{
}

wxMenuBar* CDVToolWriterFrame::createMenuBar()
{
	wxMenu* fileMenu = new wxMenu();
	fileMenu->Append(Menu_File_File, _("Open WAV File..."));
	fileMenu->AppendSeparator();
	fileMenu->Append(wxID_EXIT, _("Exit"));

	wxMenu* editMenu = new wxMenu();
	editMenu->Append(Menu_Edit_Preferences, _("Preferences..."));

	wxMenu* helpMenu = new wxMenu();
	helpMenu->Append(wxID_ABOUT, _("About DVTool Writer"));

	wxMenuBar* menuBar = new wxMenuBar();
	menuBar->Append(fileMenu, _("File"));
	menuBar->Append(editMenu, _("Edit"));
	menuBar->Append(helpMenu, _("Help"));

	return menuBar;
}

void CDVToolWriterFrame::error(const wxString& error)
{
	CErrorEvent event(error, ERROR_EVENT);

	AddPendingEvent(event);
}

void CDVToolWriterFrame::showFinished()
{
	wxCommandEvent event(FINISHED_EVENT);

	AddPendingEvent(event);
}

void CDVToolWriterFrame::onQuit(wxCommandEvent& event)
{
	Close(false);
}

void CDVToolWriterFrame::onOpen(wxCommandEvent& event)
{
	wxFileDialog dialog(this, _("Select a WAV File"), wxEmptyString, wxEmptyString, _("WAV file (*.wav)|*.wav|All files (*.*)|*.*"));
	if (dialog.ShowModal() != wxID_OK)
		return;

	wxString fileName = dialog.GetPath();

	CWAVFileReader* reader = new CWAVFileReader(fileName, DSTAR_RADIO_BLOCK_SIZE);
	bool res = reader->open();
	if (!res) {
		wxMessageDialog dialog(this, _("Cannot find the file, or the file is corrupt."), _("DVTool Writer Error"), wxICON_ERROR);
		dialog.ShowModal();
		delete reader;
		return;
	}

	unsigned int sampleRate = reader->getSampleRate();
	if (sampleRate != 48000U) {
		wxMessageDialog dialog(this, _("Invalid WAV file sample rate, not equal to 48000 sps."), _("DVTool Writer Error"), wxICON_ERROR);
		dialog.ShowModal();
		delete reader;
		return;
	}

	unsigned int channels = reader->getChannels();
	if (channels != 1U) {
		wxMessageDialog dialog(this, _("Wrong  number of channels in the WAV file."), _("DVTool Writer Error"), wxICON_ERROR);
		dialog.ShowModal();
		delete reader;
		return;
	}

	wxFileName outFileName(fileName);

	IFileWriter* writer = NULL;
	switch (m_format->GetSelection()) {
		case 0:
			outFileName.SetExt(wxT("dvtool"));
			writer = new CDVTOOLFileWriter(outFileName.GetFullPath());
			break;
		case 1:
			outFileName.SetExt(wxT("ambe"));
			writer = new CAMBEFileWriter(outFileName.GetFullPath());
			break;
		default:
			break;
	}

	if (writer == NULL) {
		wxMessageDialog dialog(this, _("No output file format specified."), _("DVTool Writer Error"), wxICON_ERROR);
		dialog.ShowModal();
		delete reader;
		return;
	}

	res = ::wxGetApp().processFile(reader, m_your->GetValue(), m_rpt1->GetValue(), m_rpt2->GetValue(), m_message->GetValue(), writer);
	if (!res) {
		wxMessageDialog dialog(this, _("Cannot process the file."), _("DVTool Writer Error"), wxICON_ERROR);
		dialog.ShowModal();
		delete reader;
	}
}

void CDVToolWriterFrame::onStart(wxCommandEvent& event)
{
	wxDateTime dateTime = wxDateTime::Now();

	wxFileName fileName;
	fileName.SetPath(wxFileName::GetHomeDir());
	fileName.SetName(dateTime.Format(wxT("%Y%m%d-%H%M%S")));

	IFileWriter* writer = NULL;
	switch (m_format->GetSelection()) {
		case 0:
			fileName.SetExt(wxT("dvtool"));
			writer = new CDVTOOLFileWriter(fileName.GetFullPath());
			break;
		case 1:
			fileName.SetExt(wxT("ambe"));
			writer = new CAMBEFileWriter(fileName.GetFullPath());
			break;
		default:
			break;
	}

	if (writer == NULL) {
		wxMessageDialog dialog(this, _("No output file format specified."), _("DVTool Writer Error"), wxICON_ERROR);
		dialog.ShowModal();
		return;
	}

	bool res = ::wxGetApp().processAudio(m_your->GetValue(), m_rpt1->GetValue(), m_rpt2->GetValue(), m_message->GetValue(), writer);
	if (!res) {
		wxMessageDialog dialog(this, _("Cannot process the audio."), _("DVTool Writer Error"), wxICON_ERROR);
		dialog.ShowModal();
		return;
	}

	m_recording->SetLabel(_("Recording"));
}

void CDVToolWriterFrame::onStop(wxCommandEvent& event)
{
	::wxGetApp().processAudio();

	m_recording->SetLabel(wxEmptyString);
}

void CDVToolWriterFrame::onPreferences(wxCommandEvent& event)
{
	wxString callsign1, callsign2;
	::wxGetApp().getCallsign(callsign1, callsign2);

	DONGLE_TYPE type;
	wxString device, address;
	unsigned int port;
	::wxGetApp().getDongle(type, device, address, port);

	wxString soundcard;
	::wxGetApp().getSoundcard(soundcard);

	CDVToolWriterPreferences dialog1(this, -1, callsign1, callsign2, type, device, address, port, soundcard);
	if (dialog1.ShowModal() != wxID_OK)
		return;

	callsign1 = dialog1.getCallsign1();
	callsign2 = dialog1.getCallsign2();
	type      = dialog1.getType();
	device    = dialog1.getDevice();
	address   = dialog1.getAddress();
	port      = dialog1.getPort();
	soundcard = dialog1.getSoundcard();

	::wxGetApp().setCallsign(callsign1, callsign2);
	::wxGetApp().setDongle(type, device, address, port);
	::wxGetApp().setSoundcard(soundcard);

	wxMessageDialog dialog2(this, _("The changes made will not take effect\nuntil the application is restarted"), _("DVTool Writer Information"), wxICON_INFORMATION);
	dialog2.ShowModal();
}

void CDVToolWriterFrame::onAbout(wxCommandEvent& event)
{
	wxAboutDialogInfo info;
	info.AddDeveloper(wxT("Jonathan Naylor, G4KLX"));
	info.SetCopyright(wxT("(C) 2014 using GPL v2 or later"));
	info.SetName(APPLICATION_NAME);
	info.SetVersion(VERSION);
	info.SetDescription(_("This program writes .ambe and .dvtool files from either\nan existing WAV file or from the microphone."));

	::wxAboutBox(info);
}

void CDVToolWriterFrame::onClose(wxCloseEvent& event)
{
	Destroy();
}

void CDVToolWriterFrame::onError(wxEvent& event)
{
	CErrorEvent& errEvent = dynamic_cast<CErrorEvent&>(event);

	wxString text = errEvent.getText();

	wxMessageDialog dialog(this, text, _("DVTool Writer Error"), wxICON_ERROR);
	dialog.ShowModal();
}

void CDVToolWriterFrame::onFinished(wxEvent& event)
{
	wxMessageDialog dialog(this, _("The file has finished processing."), _("DVTool Writer Information"), wxICON_INFORMATION);
	dialog.ShowModal();
}
